
/**
 * handles loading of new form for news
 */
function handleNewNews(){ 
	var target = 'content';
	var params = getEncodedParams({'T':'NEW'});
	loadHtmlContent(target, loadNewsFormUrl, params, initNewsArea)
}


/**
 * saves the news
 * @param		int		newsId
 */
function saveNews(newsId){
	if (checkNewsInformation()){
		var params = $('#news_de').getFormParams() + '&' + $('#news_en').getFormParams();
		if (newsId == null)
			params += '&' + getEncodedParams({'T':'NEW'});
		else
			params += '&' + getEncodedParams({'T':'EDIT', 'NEWS_ID':newsId});
		
		$.ajax({
			url: loadNewsDmlUrl,
			type: 'POST',
			dataType: 'json',
			data: params,
			success: function(response){
				if (response != null){
					if (response.SUCCESS == 1){
						setStatus(response.SUCCESS_MSG);
						setRecordId(response.NEWS_ID);
					} else
						setStatus(response.ERROR_MGS, true);
				}
			}
		});
	}
}


/**
 * checks the metainformation of news
 * @return
 */
function checkNewsInformation(){
	var isValid = checkNewsInformationByLanguage('de');
	if (isValid)
		isValid = checkNewsInformationByLanguage('en');
	return isValid;
}


/**
 * checks all fields by given language
 * @param 		string		lang
 * @return		bool		isValid
 */
function checkNewsInformationByLanguage(lang){
	var isValid = true;
	var language = (lang.toUpperCase() == 'DE') ? 'Deutsch' : 'Englisch';
	
	if (isValid && document.getElementById('news_headline_' + lang) && !(isValid= !isNull(document.getElementById('news_headline_' + lang))))
		setStatus('Headline-' + language + ' nicht gesetzt!', true);
	if (isValid && document.getElementById('news_short_info_' + lang) && !(isValid= !isNull(document.getElementById('news_short_info_' + lang))))
		setStatus('Kurzinfo-' + language + ' nicht gesetzt!', true);
	if (isValid && document.getElementById('news_display_from_' + lang) && !(isValid= !isNull(document.getElementById('news_display_from_' + lang))))
		setStatus('Anzeigen von-' + language + ' nicht gesetzt!', true);
	if (isValid && document.getElementById('news_display_to_' + lang) && !(isValid= !isNull(document.getElementById('news_display_to_' + lang))))
		setStatus('Anzeigen bis-' + language + ' nicht gesetzt!', true);
	if (isValid){
		if ($('#news_long_info_' + lang).val() == ''){
			isValid = false;
			setStatus('Info-' + language + ' nicht gesetzt!', true);
		}
	}
		
	
	return isValid;
}


/**
 * loads the form with values for existing news entry
 * @param 		int		newsId
 */
function editNews(newsId){
	var target = 'content';
	var params = getEncodedParams({'T':'EDIT', 'NEWS_ID':newsId});
	loadHtmlContent(target, loadNewsFormUrl, params, initNewsArea)
}



/**
 * loads table for all news
 */
function loadNewsTable(){
	pageSettings.saveInfo.moduleInfo.name = 'NEWS'; 
	var target = 'content';
	var params = getEncodedParams({'T':'NEWS'});
	loadHtmlContent(target, loadNewsGridUrl, params)
}


/**
 * generates the tab structe, initialises the editor and set
 * readonly attribute for english display dates
 */
function initNewsArea(){
	$('#newsTabs ul').idTabs();
	var config = {height: '450px'};
	$('.news_long_info').ckeditor(config);
	$('#news_display_from_en').attr('readonly', 'readonly');
	$('#news_display_to_en').attr('readonly', 'readonly');
	$( "#news_display_from_de" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
		});
	$( "#news_display_to_de" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd'
		});
	
	$('#news_display_from_de').change(function(){
		copyText('news_display_from_en', $(this).val());
	});
	$('#news_display_to_de').change(function(){
		copyText('news_display_to_en', $(this).val());
	});
}